package com.gd.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gd.common.entities.S_OfflineMessage;
import com.gd.common.entities.S_ResBottle;
import com.gd.common.util.ConnectionUtil;
import com.gd.server.dao.OfflineMessageDao;



public class OfflineMessageDaoImpl implements OfflineMessageDao{

	@Override
	public void insertOfflineMessage(int fromUserId, int toUserId, String message) throws Exception {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_insert = "INSERT INTO gds_offline_message(from_user_id , to_user_id, message,time) "+
				   "values(?,?,?,?)";
		pstm=conn.prepareStatement(sql_insert);
		pstm.setInt(1, fromUserId);
		pstm.setInt(2, toUserId);
		pstm.setString(3, message);
		pstm.setDouble(4, new Date().getTime());
		pstm.execute();	
		
	}

	@Override
	public List<S_OfflineMessage> getOfflineMessage(int userId) throws Exception {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_select = "SELECT * FROM `gds_offline_message` WHERE `to_user_id`=?";
		pstm = conn.prepareStatement(sql_select);
		pstm.setInt(1, userId);
		rs = pstm.executeQuery();
		
		List<S_OfflineMessage> offlineMessage = new ArrayList<S_OfflineMessage>();
		while(rs.next()){
			S_OfflineMessage msg = new S_OfflineMessage();
			msg.setId(rs.getInt(1));
			msg.setFrom_user_id(rs.getInt(2));
			msg.setTo_user_id(rs.getInt(3));
			msg.setMessage(rs.getString(4));
			msg.setTime((long)rs.getDouble(5));
			offlineMessage.add(msg);
		}
		return offlineMessage;
	}

	
	
	
}
